package gov.nasa.anml.lifted;

import java.util.ArrayList;

import gov.nasa.anml.PDDL;
import gov.nasa.anml.State;
import gov.nasa.anml.PDDL.Action;
import gov.nasa.anml.PDDL.ComplexAction;
import gov.nasa.anml.PDDL.Exists;
import gov.nasa.anml.PDDL.FloatLiteral;
import gov.nasa.anml.PDDL.Time;
import gov.nasa.anml.utility.SimpleObject;
import gov.nasa.anml.utility.SimpleString;

public class ForAll extends Block {
	
	public ForAll(Scope parent, SimpleString n) {
		super(parent,n);
	}
	
	public boolean apply(State p, int contextID, State c) {
		for(int j=0; j < 1; ++j) {
			for (int i=0;i<statements.size();++i) {
				if (!statements.get(i).apply(p,contextID,c))
					return false;
			}
		}
		return true;
	}
	public TypeCode typeCode() {
		return TypeCode.Boolean;
	}
	public ANMLBoolean value(State p) {
		for(int j=0; j < 1; ++j) {
			for (int i=0;i<statements.size();++i) {
				if (!statements.get(i).apply(p,0,null))
					return ANMLBoolean.False;
			}
		}
		return ANMLBoolean.True;
	}
	
	public void translateStmt(PDDL pddl, Interval unit, Time part) {
		translateDecl(pddl,unit);

		//bit of a hack here.
		
		ArrayList<PDDL.BooleanExpression> conditions = unit.getPDDLConditions(); 
		ArrayList<PDDL.Effect> effects = unit.getPDDLEffects();

		part = PDDL.getPart(unit,this);

		ArrayList<PDDL.BooleanExpression> myConditions = getPDDLConditions();  
		ArrayList<PDDL.Effect> myEffects = getPDDLEffects();
		ArrayList<PDDL.Parameter> myParameters = getPDDLParameters();

		for(int i=0; i < statements.size(); ++i) {
			Statement s = statements.get(i);
			s.translateStmt(pddl,this,part);
			switch(myConditions.size()) {
			case 0:
				break;
			case 1:
				conditions.add(pddl.new ForAll(myParameters,myConditions.get(0)));
				myConditions.clear();
				break;
			default:
				// swallows the array, so need a new array afterwards; this is the especially kludgy part of the whole affair
				conditions.add(pddl.new ForAll(myParameters,pddl.wrap(PDDL.Op.and,myConditions)));
				asPDDLAction.condition.arguments = myConditions = new ArrayList<PDDL.BooleanExpression>();
			}
			assert myEffects.size() == 0 : "No non-determinism.";
		}
		
	}

	
}
